import 'package:local_coffee/components/rgba.dart';
import 'package:local_coffee/components/hex.dart';
import 'package:flutter/material.dart';
import 'package:local_coffee/components/icon.dart';
import 'package:local_coffee/jsonserialize/goods_list/data.dart';
import 'package:local_coffee/utils/global.dart';

class GoodsListRow extends StatelessWidget {
  
  final bool border;
  final String activeDesc;
  final Function onPress;
  final GoodsListDatum data;

  /// 创建商品
  /// ```
  /// @param {bool} border - 是否显示底部 border
  /// @param {String} activeDesc - 活动描述
  /// @param {Function} onPress - 点击添加按钮
  /// @param {GoodsListDatum} data
  /// ```
  GoodsListRow({
    this.border = true,
    this.activeDesc,
    this.onPress,
    this.data
  });

  /// 商品信息中的文本内容
  /// ```
  /// @param {String} title
  /// @param {double} fontSize
  /// @param {FontWeight} fontWeight
  /// @param {Color} color
  /// ```
  Widget row(
    String title,
    {
      double fontSize = 11,
      FontWeight fontWeight,
      Color color
    }
  ) {
    return Row(
      children: <Widget>[
        Expanded(
          child: Text(
            title,
            style: TextStyle(
              color: color == null ? RGBA.rgbaa6a6a6 : color,
              fontSize: fontSize,
              fontWeight: fontWeight == null ? FontWeight.normal : fontWeight
            ),
          ),
        )
      ],
    );
  }

  /// 商品图片
  /// ```
  /// @param {String} imgSrc
  /// ```
  Widget goodsImg(String imgSrc) {
    return ClipRRect(
      borderRadius: BorderRadius.circular(4.0),
      child: Image.network(
        imgSrc,
        width: 70,
        height: 70,
        fit: BoxFit.cover,
      ),
    );
  }

  /// 活动信息
  /// ```
  /// @param {String} text
  /// ```
  Widget activeMsg({
    String text
  }) {
    return text == null 
      ? Container()
      : Positioned(
        right: 0,
        bottom: 5,
        child: Container(
          height: 16,
          padding: EdgeInsets.symmetric(horizontal: 4),
          alignment: Alignment.center,
          decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(2.0),
            color: RGBA.rgbaff8102
          ),
          child: Text(
            text,
            style: TextStyle(
              color: HEX.hexfff,
              fontSize: 8
            ),
          ),
        ),
      );
  }

  @override
  Widget build(BuildContext context) {
    return Row(
      children: <Widget>[
        Expanded(
          child: Container(
            padding: EdgeInsets.symmetric(vertical: 10),
            decoration: BoxDecoration(
              border: G.borderBottom(show: border)
            ),
            child: Row(
              children: <Widget>[
                Container(
                  width: 80,
                  height: 70,
                  child: Stack(
                    children: <Widget>[
                      Positioned(
                        child: goodsImg(data.pic),
                      ),
                      activeMsg(
                        text: activeDesc
                      )
                    ],
                  ),
                ),
                Expanded(
                  child: Container(
                    margin: EdgeInsets.only(left: 6),
                    child: Column(
                      children: <Widget>[
                        row(
                          data.name,
                          color: RGBA.rgba383838,
                          fontSize: 15,
                          fontWeight: FontWeight.bold
                        ),
                        row(
                          data.characteristic.isEmpty
                            ? ''
                            : data.characteristic
                        ),
                        row(""),
                        Container(
                          margin: EdgeInsets.only(top: 4),
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            crossAxisAlignment: CrossAxisAlignment.center,
                            children: <Widget>[
                              Text(
                                '￥ ${data.originalPrice}',
                                style: TextStyle(
                                  color: RGBA.rgba383838,
                                  fontSize: 15,
                                  fontWeight: FontWeight.bold
                                ),
                              ),
                              InkWell(
                                child: Container(
                                  width: 25,
                                  height: 25,
                                  child: icon0xe601(
                                    color: RGBA.rgba88afd5,
                                    size: 25
                                  ),
                                ),
                                onTap: () {
                                  onPress(context, data.id);
                                },
                              )
                            ],
                          ),
                        )
                      ],
                    ),
                  ),
                )
              ],
            ),
          ),
        )
      ],
    );
  }
}